package cn.spy.threaddownload.controller;

import cn.spy.threaddownload.exception.CustomException;
import cn.spy.threaddownload.service.DownloadServiceImpl;
import cn.spy.threaddownload.service.IDownloadService;
import cn.spy.threaddownload.model.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RequestMapping("/url")
@RestController
public class UrlDownloadController {

    @Autowired
    private IDownloadService downloadService;

    @PostMapping("/download")
    public ResultModel<String> download(@RequestParam String url) {
        ResultModel<String> resultModel = new ResultModel<>();
        try {
            String filePath = downloadService.downloadFile(url);
            resultModel.setT(filePath);
            return resultModel;
        } catch (IOException e) {
            throw new CustomException(e.getMessage());
        }
    }

}
